﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;
using Common;

namespace BLL
{
    public class MemberBLL
    {
        private MemberDAL dal = new MemberDAL();

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public List<Member> GetList()
        {
            return dal.GetList();
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="loginname"></param>
        /// <returns></returns>
        public Member GetModel(string loginname)
        {
            return dal.GetModel(loginname);
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="memid"></param>
        /// <returns></returns>
        public Member GetModel(int memid)
        {
            return dal.GetModel(memid);
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            return dal.GetList(column, where);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Add(Member model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                rm = ValidateLoginName(model);
                if (rm.code!=200)
                {
                    return rm;
                }
                int count = dal.Add(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "添加成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "添加失败,影响行数为0";
                }
            }
            catch(Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName,ex.ToString());
            }
            return rm;

        }

        /// <summary>
        /// 登陆（用于过滤器）
        /// </summary>
        /// <param name="adminuser_e">加密后的数据</param>
        /// <returns></returns>
        public ReturnMessages Login(string adminuser_e)
        {
            ReturnMessages mess = new ReturnMessages();
            try
            {
                string adminuser = Utils.AESDecrypt(adminuser_e);
                string[] adminuser_s = adminuser.Split('|');
                if (adminuser_s.Length != 2)
                {
                    mess.code = 400;
                    mess.msg = "账号不存在";
                    return mess;
                }
                string loginid = adminuser_s[0];
                string pwd = adminuser_s[1];
                Member member = dal.GetModel(loginid);
                if (member == null)
                {
                    mess.code = 400;
                    mess.msg = "账号不存在";
                    return mess;
                }

                string dtpwd = member.mem_pwd;
                if (dtpwd == pwd)
                {
                    mess.results = member;
                    mess.code = 200;
                    mess.msg = "登陆成功";
                    return mess;
                }
                else
                {
                    mess.code = 403;
                    mess.msg = "密码错误";
                    return mess;
                }
            }
            catch (Exception ex)
            {
                mess.code = 500;
                mess.msg = "服务器错误";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
                return mess;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Update(Member model)
        {
            ReturnMessages rm = new ReturnMessages();
            int count = 0;
            try
            {
                rm = ValidateLoginName(model);
                if (rm.code!=200)
                {
                    return rm;
                }
                //判断是否要修改密码
                if (!string.IsNullOrEmpty(model.mem_pwd))
                {
                    model.mem_pwd = Utils.MD5(model.mem_pwd);
                    count = dal.Update(model);
                }
                else
                {
                    count= dal.Update2(model);
                }
                count = dal.Update(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "更新成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "更新失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnMessages delete(int id)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.delete(id);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "删除成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "删除失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public ReturnMessages ValidateLoginName(Member m)
        {
            ReturnMessages rm = new ReturnMessages();
            List<dynamic> dt = GetList("*", " and mem_login='" + m.mem_login + "' and mem_id!=" + m.mem_id);
            if(dt.Count>0)
            {
                rm.code = 400;
                rm.msg="登陆名已存在！";
            }
            else
            {
                rm.code = 200;
                rm.msg = "";
            }
            return rm;
        }

    }
}
